<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="展示标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入展示标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投放页面" prop="bannerPage">
        <el-input
          v-model="queryParams.bannerPage"
          placeholder="请输入投放页面路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:banner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:banner:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:banner:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:banner:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="bannerId" />
      <el-table-column label="展示标题" align="center" prop="title" />
      <el-table-column label="资源地址" align="center" prop="imageUrl">
        <template slot-scope="scope">
          <!-- 图片类型 -->
          <el-image 
            v-if="scope.row.bannerType == CONSTANTS.BANNER_TYPE.IMAGE && scope.row.imageUrl"
            :src="getFullImageUrl(scope.row.imageUrl)" 
            style="width: 50px; height: 50px;"
            :preview-src-list="[getFullImageUrl(scope.row.imageUrl)]">
          </el-image>
          <!-- 视频类型 -->
          <div v-else-if="scope.row.bannerType == CONSTANTS.BANNER_TYPE.VIDEO && scope.row.imageUrl" class="video-preview">
            <video 
              :src="getFullImageUrl(scope.row.imageUrl)" 
              style="width: 50px; height: 50px; object-fit: cover;"
              controls>
            </video>
          </div>
          <!-- VR内容类型 -->
          <div v-else-if="scope.row.bannerType === CONSTANTS.BANNER_TYPE.VR && scope.row.imageUrl" class="vr-preview">
            <i class="el-icon-view" style="font-size: 24px; color: #409EFF;"></i>
            <span style="font-size: 12px;">VR内容</span>
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" align="center" prop="linkUrl">
        <template slot-scope="scope">
          <span v-if="scope.row.linkUrl">{{ scope.row.linkUrl }}</span>
          <span v-else style="color: #999;">-</span>
        </template>
      </el-table-column>
      <el-table-column label="跳转类型" align="center" prop="linkType">
        <template slot-scope="scope">
          <el-tag 
            v-if="scope.row.linkType && CONSTANTS.linkType_LABELS[scope.row.linkType]"
            :type="CONSTANTS.linkType_COLORS[scope.row.linkType]">
            {{ CONSTANTS.linkType_LABELS[scope.row.linkType] }}
          </el-tag>
          <el-tag v-else type="info">{{ scope.row.linkType || '-' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="展示类型" align="center" prop="bannerType">
        <template slot-scope="scope">
          <el-tag 
            v-if="scope.row.bannerType && CONSTANTS.BANNER_TYPE_LABELS[scope.row.bannerType]"
            :type="CONSTANTS.BANNER_TYPE_COLORS[scope.row.bannerType]">
            {{ CONSTANTS.BANNER_TYPE_LABELS[scope.row.bannerType] }}
          </el-tag>
          <el-tag v-else type="info">{{ scope.row.bannerType || '-' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="运营说明" align="center" prop="description">
        <template slot-scope="scope">
          <span v-if="scope.row.description">{{ scope.row.description }}</span>
          <span v-else style="color: #999;">-</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag 
            v-if="scope.row.status && CONSTANTS.STATUS_LABELS[scope.row.status]"
            :type="CONSTANTS.STATUS_COLORS[scope.row.status]">
            {{ CONSTANTS.STATUS_LABELS[scope.row.status] }}
          </el-tag>
          <el-tag v-else type="info">{{ scope.row.status || '-' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序权重" align="center" prop="sortOrder" />
      <el-table-column label="投放页面" align="center" prop="bannerPage">
        <template slot-scope="scope">
          <span v-if="scope.row.bannerPage">{{ scope.row.bannerPage }}</span>
          <span v-else style="color: #999;">-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:banner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:banner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />




  </div>
</template>

<script>
import { listBanner, delBanner } from "@/api/system/banner"
import { BASE_FILE_URL, BASE_API } from '@/utils/constants'

// 常量配置
const CONSTANTS = {
  BANNER_TYPE: {
    IMAGE: 1,
    VIDEO: 2,
    VR: 3
  },
  linkType: {
    WEB: 1,
    NATIVE: 2,
    VR_SCENE: 3
  },
  LINK_TYPE: {
    WEB: 1,
    NATIVE: 2,
    VR_SCENE: 3
  },
  STATUS: {
    DISABLED: 0,
    ENABLED: 1
  },
  BANNER_TYPE_LABELS: {
    1: '图片',
    2: '视频',
    3: 'VR内容'
  },
  linkType_LABELS: {
    1: 'Web页',
    2: '原生页面',
    3: 'VR场景'
  },
  STATUS_LABELS: {
    0: '停用',
    1: '正常'
  },
  BANNER_TYPE_COLORS: {
    1: 'success',
    2: 'primary',
    3: 'warning'
  },
  linkType_COLORS: {
    1: 'success',
    2: 'primary',
    3: 'warning'
  },
  STATUS_COLORS: {
    0: 'danger',
    1: 'success'
  }
}

export default {
  name: "Banner",
  data() {
    return {
      // 常量配置
      CONSTANTS: CONSTANTS,
      BASE_API: BASE_API, // 添加BASE_API到data中
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Banner表格数据
      bannerList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        imageUrl: null,
        linkUrl: null,
        linkType: null,
        bannerType: null,
        description: null,
        status: null,
        sortOrder: null,
        bannerPage: null
      },

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 拼接服务器图片地址 */
    getFullImageUrl(imageUrl) {
      if (!imageUrl) return '';
      // 如果已经是http/https/base64开头，直接返回
      if (/^(http|https):\/\//.test(imageUrl) || imageUrl.startsWith('data:image/')) {
        return imageUrl;
      }
      // 兼容斜杠
      if (imageUrl.startsWith('/')) {
        return this.BASE_API + imageUrl;
      }
      // 默认拼接文件服务器路径
      return BASE_FILE_URL + imageUrl;
    },
    /** 查询Banner列表 */
    getList() {
      this.loading = true
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bannerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/system/banner/add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const bannerId = row.bannerId || this.ids
      this.$router.push(`/system/banner/add?bannerId=${bannerId}`)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const bannerIds = row.bannerId || this.ids
      this.$modal.confirm('是否确认删除Banner编号为"' + bannerIds + '"的数据项？').then(function() {
        return delBanner(bannerIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/banner/export', {
        ...this.queryParams
      }, `banner_${new Date().getTime()}.xlsx`)
    },




    
    // 工具方法：获取标签类型
    getTagType(value, typeMap) {
      return typeMap[value] || 'info'
    },
    
    // 工具方法：获取标签文本
    getTagLabel(value, labelMap) {
      return labelMap[value] || value || '-'
    },
    
    // 工具方法：检查文件类型
    checkFileType(file, allowedTypes) {
      return allowedTypes.some(type => {
        if (type.endsWith('/*')) {
          return file.type.startsWith(type.slice(0, -1));
        }
        return file.type === type;
      });
    }
  }
}
</script>

<style scoped lang="scss">
.resource-upload-container {
  .resource-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    
    .resource-actions {
      display: flex;
      flex-direction: column;
      gap: 5px;
    }
    
    .vr-preview-box {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100px;
      height: 60px;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      background-color: #fafafa;
    }
  }
}

.video-preview {
  video {
    border-radius: 4px;
    border: 1px solid #e4e7ed;
  }
}

.vr-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

</style>
